package com.lottery.im.module.login.ui;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.google.gson.Gson;
import com.lottery.im.MyApplication;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.LoginEntity;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.constant.Constants;
import com.lottery.im.constant.RxBusCode;
import com.lottery.im.constant.SpKeyConstant;
import com.lottery.im.module.login.presenter.LoginFastPresenter;
import com.lottery.im.module.login.view.LoginFastView;
import com.lottery.im.util.SPUtils;
import com.lottery.im.util.StatusBarHelper;
import com.lottery.im.util.rxbus.RxBus;

/*
 * 快速登陆 ---- 验证码登陆
 * */
public class LoginFastFragment extends BaseSwipeFragment<LoginFastPresenter> implements
    LoginFastView {

  @BindView(R.id.tv_title) TextView mTvTitle;
  @BindView(R.id.et_phone) EditText mEtPhone;
  @BindView(R.id.et_code) EditText mEtCode;
  @BindView(R.id.tv_get_code) TextView mTvGetCode;

  private int mCountdownTime = Constants.COUNTDOWN_TIME;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_login_fast;
  }

  @Override protected void initPresenter() {
    presenter = new LoginFastPresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {

    StatusBarHelper.setStatusBarDarkMode(getActivity());
    RxBus.get().register(this);

    mTvTitle.setText(R.string.fast_login);
  }

  @OnClick({ R.id.iv_back, R.id.tv_get_code, R.id.btn_login_fast })
  public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        popBackStack();
        break;
      case R.id.tv_get_code:
        getCode();
        break;
      case R.id.btn_login_fast:
        login();
        break;
    }
  }

  /*
   * 获取验证码
   * */
  private void getCode() {
    String phone = mEtPhone.getText().toString().trim();

    if (TextUtils.isEmpty(phone)) {
      showToast(R.string.hint_enter_phone);
      return;
    }
    hideKeyBoard();
    presenter.getAttach(phone);
  }

  /*
   * 登录
   * */
  private void login() {
    String phone = mEtPhone.getText().toString().trim();

    if (TextUtils.isEmpty(phone)) {
      showToast(R.string.hint_enter_phone);
      return;
    }

    String code = mEtCode.getText().toString().trim();
    if (TextUtils.isEmpty(code)) {
      showToast(R.string.hint_enter_code);
      return;
    }
    hideKeyBoard();
    presenter.fastLogin(phone, code);
  }

  /*
   *  获取验证码完成
   * */
  @Override public void getCodeSuccess() {

    showToast(R.string.message_send_success);
    mTvGetCode.setEnabled(false);
    mTvGetCode.setText("重新获取" + mCountdownTime + "s");
    mTvGetCode.postDelayed(mCountdownRunnable, 1000);
  }

  /*
   * 登录成功
   * */
  @Override public void loginSuccess(LoginEntity loginEntity) {
    MyApplication.getInstance().setToken(loginEntity.getToken());
    MyApplication.getInstance().setUserInfo(loginEntity.getUserInfo());
    SPUtils.put(mActivity, SpKeyConstant.TOKEN_KEY, loginEntity.getToken());
    SPUtils.put(mActivity, SpKeyConstant.USERINFO_KEY,
        new Gson().toJson(loginEntity.getUserInfo()));
    RxBus.get().send(RxBusCode.FAST_LOGIN_SUCCESS_CODE);
  }

  /*
   *  倒计时
   * */
  private Runnable mCountdownRunnable = new Runnable() {
    @Override public void run() {

      if (null == mActivity || mActivity.isFinishing()) {
        return;
      }
      mCountdownTime--;
      if (mCountdownTime == 0) {
        mCountdownTime = Constants.COUNTDOWN_TIME;
        mTvGetCode.setEnabled(true);
        mTvGetCode.setText(getResources().getString(R.string.get_code));
      } else {
        mTvGetCode.setText("重新获取" + mCountdownTime + "s");
        mTvGetCode.postDelayed(this, 1000);
      }
    }
  };

  @Override public void onDestroy() {
    super.onDestroy();
    RxBus.get().unRegister(this);
  }
}
